﻿using Microsoft.AspNetCore.Mvc.Filters;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PmSoft
{
    //[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class TransactionFilter : IActionFilter
    {
        ISqlSugarClient _db;
        public TransactionFilter(ISqlSugarClient db)
        {
            _db = db;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            _db.AsTenant().BeginTran();
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception == null)
            {
                _db.AsTenant().CommitTran();
            }
            else
            {
                //_db.AsTenant().RollBack();
                _db.AsTenant().RollbackTran();
            }
        }
    }
}
